<template>
	<view class="container flex f-column">
		<view class="user-avatar flex">
			<open-data type="userAvatarUrl"></open-data>
		</view>
		<view class="line"></view>
		<text class="font36 gray666" style="margin-top: 100rpx;">申请获取以下权限</text>
		<text class="font30 gray999" style="margin-top: 50rpx;">获得你的公开信息（昵称、头像等）</text>
		<button class="yes-login" type="primary" @click="getUserInfo()">授权登录</button>
		<navigator open-type="navigateBack" delta="1" style="margin-top: 50rpx;">
			<view class="no-login font36 white f-center">暂不登录</view>
		</navigator>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	export default {
		data() {
			return {
				code: '',
			}
		},
		onLoad() {
			var _this = this
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					_this.code = loginRes.code
				}
			})
		},
		methods: {
			...mapMutations(['login']),
			getUserInfo() {
				let _this = this
				uni.getUserProfile({
					desc: 'Wexin', // 这个参数是必须的
					success: info => {
						_this.$http.ajax({
							url: '/User/wxlogin',
							params: {
								code: _this.code,
								gzbh: getApp().globalData.gzbh,
								appId: uni.getAccountInfoSync().miniProgram.appId
							}
						}).then((res) => {
							if (res.code == 200) {
								uni.setStorageSync('info', info.userInfo)
								uni.setStorageSync('openId', res.openid)
								uni.setStorageSync('sessionKey', res.session_key)
								uni.setStorageSync('merchantId', res.merchantId)
								uni.redirectTo({
									url: './phone'
								})
							} else {
								_this.$api.msg(res.msg)
							}
						})
					},
					fail: err => {
						_this.$api.msg('授权失败')
						setTimeout(() => {
							uni.navigateBack()
						}, 300)
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.user-avatar {
		width: 240rpx;
		height: 240rpx;
		border-radius: 50%;
		overflow: hidden;
		border: 4rpx solid #FFFFFF;
		box-shadow: 3rpx 3rpx 10rpx rgba(0, 0, 0, 0.2);
		align-self: center;
	}

	.line {
		width: 690rpx;
		height: 4rpx;
		background: #DDDDDD;
		border-radius: 2rpx;
		margin-top: 90rpx;
	}

	.yes-login {
		width: 100%;
		height: 88rpx;
		border-radius: 44rpx;
		margin-top: 120rpx;
	}

	.no-login {
		width: 100%;
		height: 88rpx;
		border-radius: 44rpx;
		background: #DFDFDF;
	}

	.container {
		padding: 60rpx 30rpx 0;
	}
</style>
